import { createRouter, createWebHistory } from 'vue-router'
import { getCurrentInstance } from "vue";

const router = createRouter({
    // mode: 'hash',
    history: createWebHistory(),
    // history: createWebHistory(),
    routes: [{
            path: '/curve/:curve_name',
            name: 'Curve',
            component: () =>
                import ('../components/Curve.vue')
        },
        {
            path: '/query/:query_name', //curve,table
            name: 'Query',
            component: () =>
                import ('../components/Query.vue')
        },
        {
            path: '/table', //curve,table
            name: 'Table',
            component: () =>
                import ('../components/Table.vue')
        },
        {
            path: '/',
            name: 'HelloWorld',
            component: () =>
                import ('../components/HelloWorld.vue')
        },
        {
            path: '/kb',
            name: 'kb',
            component: () =>
                import ('../components/NumericKeyboard.vue')
        },
        // {
        //     path: '/',
        //     component: () =>
        //         import ('../App.vue')
        // },
    ]
})

const app = getCurrentInstance()

router.beforeEach((to, from, next) => {
    // 从 url 中获取 lang 参数
    state.fontsize = to.query.fontsize || 20
    state.lang = to.query.lang || 'zh'
    state.tbid = to.query.tbid || 1
    state.db = to.query.db || 'local'
    state.ymode = to.query.ymode || 1
    state.y_click = to.query.y_click || 0
    state.y_auto = to.query.y_auto || 0
    state.freq = to.query.freq || 1
    state.last = to.query.last || 0
    state.offset = to.query.offset || 40
    state.params = to.params || {}
    state.query = to.query || {}
    if (typeof to.query.accounts === 'string') {
        state.accounts = to.query.accounts.split(',') || []
            // 继续处理 batchAccounts
    } else {
        // 处理 accounts 不是字符串的情况
        state.accounts = []
    }

    if (typeof to.query.groups === 'string') {
        state.groups = to.query.groups.split(',') || []
            // 继续处理 batchAccounts
    } else {
        // 处理 accounts 不是字符串的情况
        state.groups = []
    }
    next()
})


export const state = {
    lang: 'zh', // 默认为 zh
    fontsize: 20,
    tbid: 1,
    db: 'local',
    ymode: 1,
    y_click: 0, // Y轴是否可以被点击修改上下限
    y_auto: 0, // Y轴上下限强制使用auto
    accounts: [],
    groups: [],
    freq: 3,
    offset: 40,
    last: 0,
    params: {},
    query: {},
}

export default router